import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String again;
        do {
            System.out.println("输入一个1-100间的整数");
            int answer = (int)(Math.random()*100+1);
            int chances = 7;
            while (chances > 0){
                System.out.println("你还有"+chances+"次机会");
                int guess;
                try {
                    guess = scanner.nextInt();
                }catch (Exception e){
                    System.out.println("你输入的数字不合法");
                    scanner.next();
                    continue;
                }
                if (guess > 100 || guess < 1){
                    System.out.println("你输入的数字大于100或者小于1");
                    break;
                }else if (answer > guess){
                    System.out.println("猜小了");
                }else if (answer < guess){
                    System.out.println("猜大了");
                }else {
                    System.out.println("行了");
                    break;
                }
                chances = chances-1;
            }
            if (chances==0){
                System.out.println("你没有机会了");
            }

            System.out.println("再玩一次?  (y/n)");
            again = scanner.next();
        }while (again.equalsIgnoreCase("y"));
    }



}
